package com.jmgui.stream;

import com.alibaba.fastjson.JSONObject;
import com.jmgui.model.Person;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @InterfaceName ListToMap
 * @Description list转成map 其中v1,v2是防止重复的key 选择其中一个作为key，不然会抛出IllegalStateException: Duplicate key aa
 * @Author jmGui
 * @Date 2017/9/3 14:53
 **/
public class ListToMap {
    public static void main(String[] args) {
        List<Person> personList = Arrays.asList(new Person(1, "aa"), new Person(1, "bb"),
                new Person(2, "cc"), new Person(3, "dd"));
        Map<Object, Object> peopleMap = personList.stream().collect(Collectors.toMap(Person::getId, Person::getName,(v1,v2)->{
            return v2;
        }));
        System.out.println(JSONObject.toJSONString(peopleMap));

        Map<Integer, Person> map = personList.stream().collect(Collectors.toMap(Person::getId, e -> e,(v1,v2)->{
            return v2;
        }));
        System.out.println(JSONObject.toJSONString(map));

    }
}
